<?php

namespace Lib\Core;

abstract class Controller extends Request {

    private $params;

    private $routeParams = array();

    /**
     * @var \Lib\Core\Template
     */
    private $templateClass;

    private $tpl;

    private $templateDir = 'Template';

    public $isReturn = false;

    public function __construct() {
        $this->templateClass = new Template();
    }

	public function setStaticFileVersion($version) {
		$this->templateClass->setStaticFileVersion($version);
	}

    /**
     * 控制器默认调用的方法.
     *
     * @return void
     */
    public abstract function actionIndex();

    /**
     * 执行actionIndex前调用.
     *
     * @return void
     */
    protected function actionBefore(){}

    /**
     * 执行actionIndex后调用.
     *
     * @return void
     */
    protected function actionAfter(){}

    /**
     * 执行display前调用.
     *
     * @return void
     */
    protected function displayBefore(){}

    /**
     * 运行一个控制器.
     *
     * @param array $params
     *
     * @return void
     */
    public function run($params = array()) {
        $this->params = $params;
        if ($this->isReturn) {
            ob_start();
        }
        $this->actionBefore();
        $this->actionIndex();
        $this->actionAfter();
        if ($this->isReturn) {
            return ob_get_clean();
        }
    }

    /**
     * 获取路由的参数.
     *
     * @param string field
     *
     * @return array
     */
    public function getRouteParams($field = null) {
        $routeParams = $this->routeParams;
        if ($field !== null) {
            return isset($routeParams[$field]) ? $routeParams[$field] : null;
        }
        return $routeParams;
    }

    public function setRouteParams($params) {
        $this->routeParams = $params;
    }

    public function getUCParam($key) {
        return isset($this->params[$key]) ? $this->params[$key] : null;
    }

    public function setTemplate($tpl) {
        $this->tpl = $tpl;
    }

    /**
     * 显示一个模板.
     *
     * @return void
     */
    protected function display($tpl = null) {
        $this->displayBefore();
        if (!$tpl) {
            $tpl = $this->tpl;
        }
        if (!$tpl) {
            $class = get_called_class();
            $tpl = $this->templateDir . substr($class, 10) . '.tpl.php';
            $tpl = str_replace('\\', '/', $tpl);
        } else {
            $tpl = $this->templateDir . '/' . $tpl . '.tpl.php';
        }
        $this->templateClass->display($tpl);
    }

    protected function setTemplateDir($dir) {
        $this->templateDir = $dir;
        $this->templateClass->setTemplateDir($dir);
    }

    protected function setResourcePath($path) {
        $this->templateClass->setResourcePath($path);
    }

    /**
     * 为模板分配一个变量.
     *
     * @param string $key 变量名称.
     * @param mixed  $val 变量值.
     *
     * @return void
     */
    public function assign($key, $val) {
        $this->templateClass->assign($key, $val);
    }

    /**
     * 抛出去一个Controller异常.
     *
     * @param string  $msg  异常信息.
     * @param integer $code 异常代码.
     *
     * @throws \Lib\Exception\Controller
     */
    public function throwException($msg = '', $code = 0) {
        $code = (int) $code;
        throw new \Lib\Exception\Controller($msg, $code);
    }

}